﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace timetableasy.Models
{
    partial class Event
    {
        public bool IsValid
        {
            get
            {
                return (GetRuleViolations().Count() == 0);
            }
        }

        public IEnumerable<RuleViolation> GetRuleViolations()
        {

            if (String.IsNullOrEmpty(EventName))
                yield return new RuleViolation("Event name is required", "EventName");

            if (StartDate == null)
                yield return new RuleViolation("Start Date is required", "StartDate");

            if (EndDate == null)
                yield return new RuleViolation("End Date is required", "EndDate");

            //if (ClassRoom == null)
            //    yield return new RuleViolation("Class Room is required", "ClassRoom");

            if (EventType == null)
                yield return new RuleViolation("Event Type is required", "EventType");

            if(EventAudiance == null)
                yield return new RuleViolation("Event Audiance is required", "EventAudiance");

            yield break;
        }

        partial void OnValidate(System.Data.Linq.ChangeAction action)
        {
            if (!IsValid)
                throw new ApplicationException("Rule violations prevent saving");
        }
    }
}